package org.crazyit.album.web;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.*;

import org.crazyit.album.exception.AlbumException;
import org.crazyit.album.web.base.BaseServlet;

@WebServlet(urlPatterns = "/proRegist")
public class ProRegistServlet extends BaseServlet {
    public void service(HttpServletRequest request
            , HttpServletResponse response) throws IOException, ServletException {
        String name = request.getParameter("user");
        String pass = request.getParameter("pass");
        response.setContentType("text/javascript;charset=gbk");
        PrintWriter out = response.getWriter();
        try {
            out.println("$('#user,#pass').val('');");
            if (name != null && pass != null
                    && as.registUser(name, pass) > 0) {
                HttpSession session = request.getSession(true);
                session.setAttribute("curUser", name);
                out.println("alert('');");
                out.println("$('#noLogin').hide(500);");
                out.println("$('#hasLogin').show(500);");

                out.println("onLoadHandler();");
            } else {
                out.println("alert('');");
            }
        } catch (AlbumException ex) {
            out.println("alert('" + ex.getMessage() + "');");
        }
    }
}
